package com.hzdesk.media.koreabatchmatvector.request;

import android.content.Context;
import android.util.Log;


import com.hzdesk.media.koreabatchmatvector.bean.DeviceInfoBean;
import com.hzdesk.media.koreabatchmatvector.inter.ICallBack;
import com.hzdesk.media.koreabatchmatvector.utils.JsonUitl;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by Administrator on 2018/5/24.
 */

public class GetDeviceInfoRequest {
    private static final String TAG = GetDeviceInfoRequest.class.getSimpleName();
    private Model model = Model.getInstance();
    private Context context;
    private String Url;
    private OnGetDeviceInfoListener onResponseEventListener;

    public GetDeviceInfoRequest(Context context, String url) {
        this.context = context;
        this.Url = url;
    }

    public interface OnGetDeviceInfoListener {
        void success(DeviceInfoBean bean);

        void fail(String s);
    }

    private String Success = "SUCCESS";
    private String Fail = "FAIL";

    public void setOnGetDeviceInfoListener(OnGetDeviceInfoListener onResponseEventListener) {
        this.onResponseEventListener = onResponseEventListener;
        getRequest(Url);
    }


    public void getRequest(String url) {
        model.getSynchronized(context, url, new ICallBack() {
            @Override
            public void result(String s) {
                //System.out.println(s);
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    if (jsonObject.has("message")) {
                        if (jsonObject.getString("message").equals(Success)) {
                            onResponseEventListener.success((DeviceInfoBean) JsonUitl.stringToObject(s, DeviceInfoBean.class));
                        }
                        if (jsonObject.getString("message").equals(Fail)) {
                            onResponseEventListener.fail(s);
                        }
                    }
                } catch (JSONException e) {
                    //e.printStackTrace();
                    Log.d(TAG,s);
                }

            }
        });
    }


}
